﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FrankensteamClient
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {

        public LoginWindow()
        {
            InitializeComponent();

        }

        private void btnRegUser_Click(object sender, RoutedEventArgs e)
        {
            ClientAPI.Model.Instance.Connect();
            //System.Diagnostics.Debug.WriteLine("Waiting for connection");
            while (!ClientAPI.Model.Instance.IsConnected()) ;
            //System.Diagnostics.Debug.WriteLine("connection made");
            ClientAPI.Model.Instance.Create(txtRegUsername.Text, txtRegFirstName.Text, txtRegLastName.Text, txtRegPassword.Text, "", txtRegEmail.Text);
            ClientAPI.Model.Instance.LogOut();
            MessageTextBox("Registered", "Alert", false);
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            ClientAPI.Model.Instance.Connect();

            while (!ClientAPI.Model.Instance.IsConnected()) ;

            if (ClientAPI.Model.Instance.Login(txtUsername.Text, txtPassword.Text))
            {

                MainWindow mainWindow = new MainWindow();

                mainWindow.Show();
                this.Close();
            }
            else
            {
                ClientAPI.Model.Instance.LogOut();
                MessageTextBox("Incorrect Login: Please check login information", "Login Error", true);
            }

        }

        private void MessageTextBox(string text, string caption, bool warning)
        {
            MessageBoxImage icon;
            MessageBoxButton button = MessageBoxButton.OKCancel;

            if (warning)
                icon = MessageBoxImage.Error;
            else
                icon = MessageBoxImage.Information;

            MessageBox.Show(text, caption, button, icon);
        }
    }
}
